package org.theqcp.wow.sim.gui.tabs.imagethings;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.OverlayLayout;

import org.theqcp.wow.sim.gui.tabs.TalentsPanel.Talent;

public class TalentButton extends JPanel {

    private static final long serialVersionUID = 7003540239038926276L;

    protected final JLabel rankLabel = new JLabel();

    protected final Image cover, img;
    protected final Talent talent;
    protected final int offset;
    protected int rank = 0;

    public TalentButton(MouseListener listener, Talent talent, Image coverImage, int width, int height, int offsetD) {
	super();

	final Dimension size = new Dimension(width, height);
	this.offset = offsetD;

	this.img = talent.getImage(width - offset, height - offset, Image.SCALE_SMOOTH);
	this.cover = coverImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
	this.talent = talent;

	// setBorder(BorderFactory.createLineBorder(Color.green));
	setPreferredSize(size);
	setMinimumSize(size);
	setMaximumSize(size);
	setSize(size);

	setLayout(new OverlayLayout(this));
	addMouseListener(listener);

	// p.setAlignmentX(LEFT_ALIGNMENT);
	// p.setAlignmentY(TOP_ALIGNMENT);
	// p.setOpaque(true);

	rankLabel.setBackground(Color.black);
	rankLabel.setOpaque(true);
	rankLabel.setAlignmentX(RIGHT_ALIGNMENT);
	rankLabel.setAlignmentY(BOTTOM_ALIGNMENT);

	add(rankLabel);

	setRank(0);
    }

    @Override
    protected void paintComponent( Graphics g ) {
	g.drawImage(img, offset / 2, offset / 2, null);
	if( cover != null ) {
	    g.drawImage(cover, 0, 0, null);
	}
    }

    public int getRank() {
	return rank;
    }

    public int getMaxRank() {
	return talent.getMaxRank();
    }

    public void setRank( int rank ) {
	this.rank = rank;
	rankLabel.setText(" " + String.valueOf(rank) + " ");

	if( rank == talent.getMaxRank() ) {
	    rankLabel.setBorder(BorderFactory.createLineBorder(Color.yellow));
	    rankLabel.setForeground(Color.yellow);
	} else {
	    rankLabel.setBorder(BorderFactory.createLineBorder(Color.green));
	    rankLabel.setForeground(Color.green);
	}
    }

    public void incRank() {
	if( rank < talent.getMaxRank() ) {
	    setRank(rank + 1);
	} else {
	    setRank(talent.getMaxRank());
	}
    }

    public void decRank() {
	if( rank > 0 ) {
	    setRank(rank - 1);
	} else {
	    setRank(0);
	}
    }

}
